<?php
/**
 *@todo 订单礼品管理
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2016-1-25
 +------------------------------------------------------------------------------
 */
namespace Admin\Controller;
class OrderGiftController extends AdminController {
    
    /**
     * @todo 订单换礼品列表
     * @author sap
     * @since 2016-1-25
     */
    public function index(){
        $present_id = (int)I('present_id');//礼品ID号
        $activity_id = (int)I('activity_id');//活动ID号
        $paper_order = (int)I('paper_order');//纸质订单号
        $mobile_phone = trim(strip_tags(I('mobile_phone')));//手机号
        //查询条件
        $map = array();
        if($paper_order != 0){
            $gift_id = M('OrderGiftPaper')->where(array('paper_order'=>$paper_order))->getField('gift_id');
            $map['gift_id'] = $gift_id;
        }
        if(!empty($mobile_phone)){
            $callback = D('Admin/Customer','Service')->getCustomerIdByMobilePhone($mobile_phone);
            if($callback['status'] == 1){
                $customer_id = (int)$callback['data']['customer_id'];
                $map['customer_id'] = $customer_id;
            }
        }
        if($present_id != 0){
            $map['present_id'] = $present_id;
        }
        if($activity_id != 0){
            $map['activity_id'] = $activity_id;
        }
        //列表
        if(!empty($map)){
            if($mobile_phone && !isset($customer_id)){
                $list = null;
            }elseif ($paper_order!=0 && !isset($gift_id)){
                $list = null;
            }else{
                $map['status'] = 0;
                $list = $this->lists('OrderGift',$map,'gift_id desc','gift_id,present_id,customer_id,activity_id,gift_num,change_time,back_time,status');
//                 int_to_string($list,array('status'=>array(0=>'换礼',1=>'还礼')));
                foreach ($list as $key=>$val){
                    $list[$key]['present_name'] = getPresentTitle($val['present_id'],$val['activity_id']);
                }
            }
        }else{
            $list = null;
        }
        $this->assign('_list',$list);
        $this->getActivityList(0);//线下活动列表
        $this->getPresentList($activity_id);//礼品列表
        $this->assign('meta_title','订单换礼管理');
        $this->display('index');
    }
    
    /**
     * @todo 订单换礼详情
     * @author sap
     * @since 2016-2-19
     */
    public function orderGiftInfo(){
        $gift_id = (int)I('gift_id');
        $gift_info = M('OrderGift')->where(array('gift_id'=>$gift_id,'status'=>0))->field(true)->find();//订单换礼详情
        if(!empty($gift_info)){
            $gift_info['present_name'] = getPresentTitle($gift_info['present_id'],$gift_info['activity_id']);
        }
        $gift_order_list = M('OrderGiftPaper')->where(array('gift_id'=>$gift_id))->field(true)->select();//换礼纸质订单列表
        $this->assign('gift_info',$gift_info);
        $this->assign('gift_order_list',$gift_order_list);
        $this->assign('meta_title_c','订单换礼管理');
        $this->assign('meta_title','订单换礼详情');
        $this->display('info');
    }
    
    /**
     * @todo 添加订单换礼
     * @author sap
     * @since 2016-1-25
     */
    public function add(){
        if(IS_POST){
            $obj = D('Admin/Order');
            $data = $obj->addOrderGift();
            if(empty($data)){
                $this->error($obj->getError());
            }
            $this->success('操作成功');
        }else{
            $activity_id = (C('JJHH_ACTIVITY_ID') > 0)? C('JJHH_ACTIVITY_ID') : 0 ;//默认活动ID
            $this->assign('activity_id', $activity_id);
            $this->getActivityList(0);//线下活动列表
            $this->getPresentList($activity_id);//礼品列表
            $this->assign('meta_title','订单换礼');
            $this->display('add');
        }
    }
    
    /**
     * @todo 按纸质订单号搜索订单信息
     * @author sap
     * @since 2016-2-18
     */
    public function searchOrder(){
        $activity_id = (int)I('activity_id');
        $paper_order = strip_tags(I('paper_order'), '');
        if(empty($paper_order)) {
            $this->error('【请扫描纸质订单】或【输入纸质订单号并回车】');
        }
        if($activity_id == 0){
            $this->error('【请选择活动】');
        }
    
        $data = D('Admin/Order')->getOrderShop(array('activity_id'=>$activity_id,'paper_order'=>$paper_order),'order_id,customer_id,paper_order,order_amount,conf_category_id');
        if(!empty($data)){
            $data['conf_category'] = getConfCategoryName($data['conf_category_id']);
            //判断在该品类下是否已经换过礼
            $map = array();
            $map['activity_id'] = $activity_id;
            $map['customer_id'] = $data['customer_id'];
            $map['category_id'] = $data['conf_category_id'];
            $order_gift_paper_info = D('OrderGiftPaperView')->where($map)->field('paper_order')->select();
            $data['paper_gift_status'] = '<font color="green">在该品类下未换礼</font>';
            if(!empty($order_gift_paper_info)){
                $data['paper_gift_status'] = '<font color="red">在该品类下已换礼</font>';
            }
        }
    
        if(empty($data)){
            $return['status'] = 0;
            $return['info'] = '【暂无订单信息】';
        }else{
            $return['status'] = 1;
            $return['data'] = $data;
        }
        $this->ajaxReturn($return);
    }
    
    /**
     * @todo 删除订单换礼记录
     * @author sap
     * @since 2016-2-19
     */
    public function del(){
        if(IS_POST){
            $obj = D('Admin/Order');
            $result = $obj->delOrderGift();
            if(empty($result)){
                $this->error($obj->getError());
            }else{
                $this->success('操作成功',U('OrderGift/del'));
            }
        }else {
            $this->meta_title = '订单还礼';
            $this->display('del');
        }
    }
    
    /**
     * @todo 订单还礼时显示的订单礼品详细信息
     * @author sap
     * @since 2016-3-28
     */
    public function getOrderGiftInfo(){
        if(IS_POST){
            $obj = D('Admin/Order');
            $order_gift = $obj->getOrderGift();//根据纸质订单号查询换礼记录，及该记录下的所有纸质订单号
            $result = array();
            if(!empty($order_gift)){
                $result['status'] = 1;
                $result['info'] = $order_gift;
            }else{
                $result['status'] = 0;
                $result['info'] = $obj->getError();
            }
            $this->ajaxReturn($result);
        }
    }

    /**
     * @todo 活动列表
     * @param $is_online 0|1
     * @author sap
     * @since 2016-1-26
     */
    private function getActivityList($is_online=0){
        $activity_list = D('Activity','Service')->getActivityList($is_online);
        $this->assign('activity_list',$activity_list);
    }
    
    /**
     * @todo 礼品列表
     * @param $activity_id
     * @author sap
     * @since 2016-3-5
     */
    private function getPresentList($activity_id=0){
        $present_id_list = null;
        if($activity_id > 0){
            $present_id_array = C('PRESENT_ID');
            $present_id_list = isset($present_id_array[$activity_id]) ? $present_id_array[$activity_id] : null ;
        }
        $this->assign('present_id_list',$present_id_list);
    }
    
    /**
     * @todo 根据活动ID读取活动下礼品列表(公共方法-活动礼品联动时使用，不做权限限制)
     * @author sap
     * @since 2016-2-23
     */
    public function getPresentId(){
        $activity_id = (int)I('activity_id');
        $present_id_list = null;$data = array();
        if($activity_id != 0){
            $present_id_array = C('PRESENT_ID');
            $present_id_list = isset($present_id_array[$activity_id]) ? $present_id_array[$activity_id] : null ;
        }
        if(empty($present_id_list)){
            $data['status'] = 0;
            $data['info'] = '没有数据';
        }else{
            $data['status'] = 1;
            $data['data'] = $present_id_list;
        }
        $this->ajaxReturn($data);
    }
}